package cn.edu.tju.elm.common.response;


import org.springframework.http.HttpStatus;

import java.io.Serializable;

public class HttpResult <T> implements Serializable {

    /**
     * 是否响应成功
     */
    private boolean success;
    /**
     * 响应状态码
     */
    private int code;
    /**
     * 响应数据
     */
    private T data;
    /**
     * 错误信息
     */
    private String message;

    // 构造器开始
    /**
     * 全参构造器
     */
    public HttpResult(int code, String message, T data, boolean success) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.success = success;
    }

    public HttpResult() {
    }


    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    // 构造器结束

    public static<T> HttpResult<T> ok (){
        return new HttpResult<>(HttpStatus.OK.value(),null,null,true);
    }

    public static<T> HttpResult<T> ok (String message, T data){
        return new HttpResult<>(HttpStatus.OK.value(),message,data,true);
    }

    public static<T> HttpResult<T> ok ( T data){
        return new HttpResult<>(HttpStatus.OK.value(),HttpStatus.OK.getReasonPhrase(),data,true);
    }

    public static<T> HttpResult<T> failure(int code, String message){
        return new HttpResult<>(code,message,null,false);
    }

    public static<T> HttpResult<T> failure(int code){
        return new HttpResult<>(code,HttpStatus.valueOf(code).getReasonPhrase(),null,false);
    }

    @Override
    public String toString() {
        return "HttpResult{" +
                "success=" + success +
                ", code=" + code +
                ", data=" + data +
                ", message='" + message + '\'' +
                '}';
    }
}

